Thumb

What is switch?

1/8/2020 6:10:57 AM

Switch is a conditional statement like if else. When we use switch statement then we use case. When we use switch then each value called case. Then every switch value matches the case then when match the case then this block of code executes otherwise default block execute. Now given bellow the switch case code and explain the code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Reflection;
using testForClass1;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace testFor
{
    public class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter Your Name : ");
            string name = Console.ReadLine();
            switch (name)
            {
                case "jesy":
                    Console.WriteLine("My Name is Jesy");
                    break;
                case "reza":
                    Console.WriteLine("My Name is Reza");
                    break;
                default: /* Optional */
                    Console.WriteLine("Not Match");
                    break;
            }
            Console.Read();
        }
    }
}

In this code we use the switch statement. First take a user input as a name then we match the case of the name variable when match the case the execute the block of code otherwise default block is executing.